<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<div class="container_12 clearfix leading" style="margin-bottom: 0px">
	<section class="portlet grid_12 leading">

		<header>
			<h2>打分周期和条件筛选</h2>
		</header>

		<section>
			<p>
				筛选： <select name="sel_periodtype" id="sel_periodtype" style="opacity: 0;" onchange="loadLastThirdExamPeriod(this)">
					<option value="monthly">月度</option>
					<option value="bimonthly">双月</option>
					<option value="season">季度</option>
					<option value="semiyearly">半年</option>
					<option value="annually">一年</option>
				</select>
                <select name="sel_period" id="sel_period" style="opacity: 0;">
					<option value="-1">所有周期</option>
				</select><span> <select name="sel_area" id="sel_area"
					style="opacity: 0;" onchange="loadChildrenOrgsByTab(this)">
					<option value="-1">所有分公司</option>
					<s:iterator value="#request.organizations" var="orgs" status="st">
						<option value="${orgs.id}">${orgs.fullName}</option>
					</s:iterator>
				</select></span>
				<span><input type="button" value=" 提交 " onclick="loadControlPage('${listAction}')" /> </span>
			</p>
		</section>

	</section>
	<div class="tabs side grid_12 leading">

		<div class="clear"></div>
	</div>
</div>
<script type="text/javascript">
	var orgstring = "";
	var orgs;
	$(function(){
	    setTimeout("init()", 600);
	});

	function init(){
	    if("${type}" == ""){
	        initPeriodByType("monthly");
	        return;
	    }
	    $("#sel_periodtype").val("${type}");
	    initPeriodByType("${type}");
	    $("#sel_period").val("${periodDesc}");
	    var temp = "${periodDesc}";
	    $("#sel_period").prev("span").html(temp == "-1" ? "当前考核周期" : temp.replace("-","年").concat("月"));
	    
	    //加载组织结构
	    if("${organizationIds}" != ""){
	    	orgstring = "," + "${organizationIds}" + ",";
	    	orgs = "${organizationIds}".split(",");
	    	$("#sel_area").val(orgs[0]);
			$("#sel_area").prev("span").html($("#sel_area").find("option:selected").text());
			loadChildrenOrgsByTab(document.getElementById("sel_area"));
	    }
	}
	
	function loadLastThirdExamPeriod(tarObj){
		var ptype = tarObj.options[tarObj.selectedIndex].value;
        if(true){
            initPeriodByType(ptype);
            return;
        }

		//periods
		$.ajax({
	        type:"get",
	        url:"/ajax/period_list.xhtml?periodEnum=" + ptype,
	        //data: "name=John&location=Boston",
	        dataType:"json",
	        success:function (data) {
	        	var strHTML = "<option value=\"-1\">当前考核周期</option>";
                $.each(data, function (i, period) {
                    strHTML += "<option value=\"" + period.id + "\">" + org.periodName + "</option>";
                });
                $("#sel_period").html(strHTML);
	        }
	    });
	}

	var currentSelOrgId = -1;
	function loadChildrenOrgsByTab(targetObj) {
		currentSelOrgId = targetObj.options[targetObj.selectedIndex].value;
		$(targetObj).prev().html($(targetObj).find("option:selected").text());
		$(targetObj).parent().nextAll().remove();
		if(currentSelOrgId != -1){
		    //load the organization uniform-sel_area 
		    $.ajax({
		        type:"get",
		        url:"/ajax/organization-children.xhtml?parentId=" + currentSelOrgId,
		        //data: "name=John&location=Boston",
		        dataType:"json",
		        success:function (data) {
		        	if(data != null && data.length > 0){
			        	var strHTML = " <div id=\"uniform-sel-area-" + currentSelOrgId + "\" class=\"selector\">";
	                    strHTML += "<span style=\"-moz-user-select: none;\">所有</span>";
	                    strHTML += "<select id=\"sel_area-" + currentSelOrgId + "\" name=\"sel_area-" + currentSelOrgId + "\" style='opacity: 0;' onchange='loadChildrenOrgsByTab(this)'>";
	                    strHTML += "<option value=\"-1\">所有</option>";
	                    $.each(data, function (i, org) {
							strHTML += "<option value=\"" + org.id + "\">" + org.fullName + "</option>";
	                    });
	                    strHTML += "</select></div>";
	                    $(targetObj).parent().after(strHTML);
	                    // load the default child org selected
	                    if(orgstring != ""){
	                    	setTimeout("updateChildOrgTabs('sel_area-" + currentSelOrgId + "')",600);
	                    }
		        	}
		        }
		    });
		}
		if(currentSelOrgId <= 0){
			currentSelOrgId = $(targetObj).parent().prev().find("option:selected").val();
		}
	}
	
	function updateChildOrgTabs(objId){
        var selObj = document.getElementById(objId);
		for(var i=0;i<selObj.options.length;i++){
        	if(orgstring.indexOf("," + selObj.options[i].value + ",") >= 0){
        		selObj.options[i].selected = true;  //保持选中状态
        	}
        }
		if($("#" + objId).val() != "-1"){
        	$("#" + objId).prev("span").html($("#" + objId).find("option:selected").text());
        	loadChildrenOrgsByTab(document.getElementById(objId));
        }
		
		if($("#" + objId).val() == orgs[orgs.length - 1]){
        	loadControlPage('${listAction}');
		}
	}

    var bimonthlys_p = ["11~12","1~2","3~4","5~6","7~8","9~10"];
    var seasons_p = ["10~12","1~3","4~6","7~9"];
    var semiyearly_p = ["7~12","1~6"];
    function initPeriodByType(type){
        if(type == "none"){
            $("#sel_period").prev("span").html("所有周期");
            return;
        }

        var myDate = new Date();
        var d = myDate.getDate(); //1~31
        var m = myDate.getMonth();//0~11
        var y = myDate.getFullYear();//2012
        var options = new MyObj(type, m+1, y).options;

        if(options.length > 0){
            var obj = $("#sel_period");
            obj.html("");
            for(var i=0; i<options.length; i++){
                obj.append("<option value=\"" + options[i].value + "\">" + options[i].title + "</option>")
            }
            $("#sel_period").prev("span").html(options[0].title);
        }
    }

    function MyObj(type, mon, year){
        this.type = type;
        this.m = mon;
        this.y = year;
        
        function getOptionsByType(type, mon, year){
            var options = new Array();
            //options.push({"title":"当前考核周期","value":"-1"});
            if(type == "monthly"){//单月
                options.push({"title":"当前考核周期","value":"-1"});
                var j = 0;
                var mm = mon - 2;
                var yy = year;
                while(j < 3){
                    if(mm <= 0){
                        mm = 12 + mm;
                        yy = yy - 1;
                    }
                    options.push({"title":yy + "年" + mm + "月","value":yy + "-" + mm});
                    //options.push('<option value="'+yy+'_'+mm+'">'+yy+'年'mm+'月</option>');
                    j++;
                    mm--;
                }

            }else if(type == "bimonthly"){//双月
                var yy = year;
                var index = (mon%2==0) ? (mon-1)/2 : mon/2;
                index = parseInt(index);
                var j = 0;
                if(mon%2 == 1){
                    options.push({"title":"当前考核周期","value":"-1"});
                    index--;
                    if(index < 0){
                        index = 5;
                        yy--;
                    }
                }

                while(j < 3){
                    if(index <= 0){
                        yy--;
                    }
                    options.push({"title":yy+"年"+bimonthlys_p[index]+"月","value":yy + "-" + bimonthlys_p[index].split("~")[1]});
                    j++;
                    if(index <= 0){
                        index = 5;
                    }else{
                        index--;
                    }
                }
            }else if(type == "season"){//季度
                var yy = year;
                var temp = mon%3;
                if(temp == 0){
                    index = mon/3 -1;
                }else if(temp == 1){//1,4,7,10可考核
                    index = mon/3;
                }else{
                    index = (mon-temp)/3;
                }
                index = parseInt(index);
                var j = 0;
                if(temp == 1){
                    options.push({"title":"当前考核周期","value":"-1"});
                    index--;
                    if(index < 0){
                        index = 3;
                        yy--;
                    }
                }
                while(j < 3){
                    if(index <= 0){
                        yy--;
                    }
                    options.push({"title":yy+"年"+seasons_p[index]+"月","value":yy+"-"+seasons_p[index].split("~")[1]});
                    j++;
                    if(index <= 0){
                        index = 3;
                    }else{
                        index--;
                    }
                }
            }else if(type == "semiyearly"){//半年
                var yy = year;
                var temp = mon%6;
                if(temp == 0){
                    index = mon/6 -1;
                }else if(temp == 1){//1,7可考核
                    index = mon/6;
                }else{
                    index = (mon-temp)/6;
                }
                index = parseInt(index);
                var j = 0;
                if(temp == 1){
                    options.push({"title":"当前考核周期","value":"-1"});
                    index--;
                }
                if(index < 0){
                    index = 1;
                    yy--;
                }
                while(j < 3){
                    if(index <= 0){
                        yy = yy - 1;
                    }
                    options.push({"title":yy+"年"+semiyearly_p[index]+"月","value":yy+"-"+semiyearly_p[index].split("~")[1]});
                    if(index <= 0){
                        index = 2;
                    }
                    j++;
                    index--;
                }
            }else if(type == "annually"){//一年
                var yy = year;
                if(mon == 1){//仅一月可考
                    options.push({"title":"当前考核周期","value":"-1"});
                    yy--;
                }
                for(var i=0; i<3; i++){
                    yy--;
                    options.push({"title":yy+"年1~12月","value":yy + "-12"});
                }
            }
            return options;
        }

        this.options=getOptionsByType(this.type, this.m, this.y);
    }

</script>